﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNViewParseMasterDetialWorker
	{
		
		public static void  parseMasterDetial(View view)
		{
			string xdPrefix = view.XdPrefix;
			string xslPrefix = view.XslPrefix;
			XmlElement root = view.Doc.DocumentElement;
			string masterNameFilter = "//@" + NamespaceUtil.toFullName(xdPrefix, "masterName");
			XmlNodeList masterNameAttrList = DOMUtil.selectNodes(root, masterNameFilter, 0, view.NsResolver);

            //CID 2083 //2014.01.17 //Begin
            Hashtable masterTable = new Hashtable();
            //CID 2083 //2014.01.17 //End
			
			ArrayList masterNameList = new ArrayList();
			foreach (XmlAttribute masterNameAttr in masterNameAttrList)
			{
				string masterName = masterNameAttr.Value;

				bool found = false;
				foreach (string name in masterNameList)
				{
					if (masterName.Equals(name))
					{
						found = true;
						break;
					}
				}
				if (found)
				{
					continue;
				}
				masterNameList.Add(masterName);
                //CID 2083 //2014.01.17 //Begin
                HashtableUtil.addSafety(masterTable, masterName, masterNameAttr.OwnerElement);
                //CID 2083 //2014.01.17 //End
			}
			
			view.MasterNameList = masterNameList;

			//查找CtrlID和MasterID的匹配定义，用于FormMasterDetailContextWorker中将CtrlID转换为MasterID。
			string linkedToMasterFilter = "//@" + NamespaceUtil.toFullName(xdPrefix, "linkedToMaster");
			XmlNodeList linkedToMasterAttrList = DOMUtil.selectNodes(root, linkedToMasterFilter, 0, view.NsResolver);
			
			Hashtable masterLinkTable = new Hashtable();
			foreach( XmlAttribute linkedToMasterAttr in linkedToMasterAttrList )
			{
				string masterID = linkedToMasterAttr.Value;

				XmlAttribute ctrlIdAttr = linkedToMasterAttr.OwnerElement.GetAttributeNode( NamespaceUtil.toFullName(xdPrefix, "CtrlId") );
				if( ctrlIdAttr != null )
				{
					HashtableUtil.addSafety( masterLinkTable, ctrlIdAttr.Value, masterID );
                    //CID 2083 //2014.01.17 //Begin
                    if (ConfigUtil.EnableMasterDetailsPopup())
                    {
                        string detailId = ctrlIdAttr.Value;
                        bool foundDetailXmlToEdit = false;
                        foreach (XmlToEdit xmlToEdit in view.Manifest.XmlToEditList)
                        {
                            if (xmlToEdit.ViewName.Equals(view.Name, StringComparison.Ordinal))
                            {
                                if (xmlToEdit.ViewContext.Equals(detailId))
                                {
                                    foundDetailXmlToEdit = true;
                                    break;
                                }
                            }
                        }
                        if (!foundDetailXmlToEdit)
                        {
                            System.Diagnostics.Debug.WriteLine("detail not insertable");
                            XmlElement masterElement = masterTable[masterID] as XmlElement;
                            masterElement.SetAttribute("masterDetailsPopup", "true");

                            XmlElement detailsElement = linkedToMasterAttr.OwnerElement;
                            string style = detailsElement.GetAttribute("style");
                            style = style + ";display:none";
                            detailsElement.SetAttribute("style", style);
                            string classes = detailsElement.GetAttribute("class");
                            classes = classes + " xdPopupDetails";
                            detailsElement.SetAttribute("class", classes);

                            //CID 2097 //2014.01.26 //Begin
                            if (view.PopupDetailsMasterNames == null)
                            {
                                view.PopupDetailsMasterNames = new System.Collections.Generic.List<string>();
                            }
                            view.PopupDetailsMasterNames.Add(masterID);
                            //CID 2097 //2014.01.26 //End
                        }
                    }
                    //CID 2083 //2014.01.20 //End
				}
			}

			view.MasterLinkTable = masterLinkTable;
		}
	}
}